<template>
	<view>	
	<view class="searchLayout">
		<u-search 
		shape="round"
		:clearabled="true"
		placeholder="请输入搜索内容" 
		:showAction="true" 
		actionText="搜索" 
		:animation="true"
		@search="onSearch"
		@custom="onSearch"
		@clear="clearSearch"
		v-model="keyword"></u-search>
	
	<view class="historyList" v-if="goodsData.length == 0">
		<view class="item" v-for="(item,index) in historyList" :key="index" >
			<view class="text" @click="clickHitTEXT(item)">{{item}}</view>
			<view class="close">
				<u-icon name="close" size="24" @click="onClose(index)"></u-icon>
				
			</view>
		</view>
	</view>
	
	<view class="goodsLayout">
		<view class="item" v-for="item in goodsData" :key="item._id">
			<product-item :item="item"></product-item>
		</view>
	</view>	
	</view>
	<pro-detail-popup></pro-detail-popup>
	<pro-select-specs></pro-select-specs>
	<view class="carLayout" v-if="buynum > 0" >
		<car-layout ></car-layout>
	</view>
	
	<view v-if="noData">
		<u-empty
		        mode="search"
		        icon="http://cdn.uviewui.com/uview/empty/search.png"
		>
		</u-empty>

		
	</view>
	
	</view>
</template>

<script>
	const goodsCloudObj = uniCloud.importObject("wanshop-mall-goods")
	import {
		mapState,
		mapMutations,
		mapGetters
	} from "vuex"
	export default {
		data() {
			return {
				keyword:"",
				historyList:[],
				goodsData:[],
				noData:false
			};
		},
		onLoad() {
			let historyList = uni.getStorageSync("historyList") || []
			this.historyList = historyList
			
		},
		computed:{
			...mapGetters(["foldState","buynum","carsList"])
		},
		watch:{
			// carsList(nv,ov){
			
			// },
			carsList:{
				deep:true,
				handler(){
					this.setNumValue();	
				}
			}
		},
		methods:{
			onSearch(){
				this.historyList.unshift(this.keyword)
				this.historyList = [...new Set(this.historyList)]
				uni.setStorageSync("historyList",this.historyList)
				this.getData()
			},
			onClose(index){
				console.log(index);
				this.historyList.splice(index,1);
				uni.setStorageSync("historyList",this.historyList)
			},
			clickHitTEXT(value){
				this.keyword = value;
				this.getData()
			},
			clearSearch(){
				
			},
			async getData(){
				let res = await goodsCloudObj.searchGoods(this.keyword)
				console.log(res);
				this.goodsData = res.data;
				this.setNumValue();
				if(this.goodsData.length){
					this.noData = false
				}else{
					this.noData = true
				}
			},
			setNumValue() {
			  // 初始化 numvalue
			  this.goodsData.forEach(item => {
			    this.$set(item, "numvalue", 0);
			  });
			
			  // 设置特定商品的 numvalue
			  this.carsList.forEach((item1,index1) => {
			    let index = this.goodsData.findIndex((item2,index2)=>{
					return item2._id == item1.goodsid
				});
			
			    if (index >= 0) {
			      this.$set(this.goodsData[index], 'numvalue', item1.numvalue);
			    }
			  });
			}
		}
	}
</script>

<style lang="scss">
	.searchLayout{
		padding: 30rpx;
		padding-bottom:150rpx ;
		.historyList{
			margin-top: 30rpx;
			.item{
				@include flex-box();
				font-size: 32rpx;
				padding: 30rpx 0;
				color: #333;
				border-bottom: 1 rpx solid $border-color-light;
			}
		}
	}
	.carLayout{
		position: fixed;
		bottom: 0;
		left:0;
		width: 100%;
	}
</style>
